import numpy as np
import csv
from datetime import datetime
from time import strftime


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = int(float(record[item]))
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def cal_dist(x1, y1, x2, y2):
    temp_dist = (float(x1 - x2))**2 + (float((y1 - y2))**2)
    #print x1, y1, x2, y2, (float(x1 - x2))**2, (float((y1 - y2))**2), temp_dist
    temp_dist = temp_dist**0.5
    #print temp_dist

    return temp_dist

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    filepath = 'C:/_DATA/migration_census_2000/'
    #filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/test/US_census_pop_2011_county_id.csv'

    dataCSV = filepath + 'census_county_migration_format.csv'
    data = build_data_list(dataCSV)

    #idtableCSV = filepath + 'REDCAPLUS_100Region/regionalization_Full-Order-ALK_POP95_min_0_100reg.csv'
    idtableCSV = filepath + 'census_county_pop_state_fid.csv'
    idtable = build_data_list(idtableCSV)

    #Centroid_CSV = filepath + 'REDCAPLUS_100Region/redcaplus_100_centroid.csv'
    Centroid_CSV = filepath + 'STATE/continental_cnty_state_centroid.csv'
    centroid = build_data_list(Centroid_CSV)

    n = len(centroid)

    distance = np.zeros((n,n))
    
    for i in range(len(centroid)-1):
        for j in range(i+1, len(centroid)):
            distance[i,j] = int(cal_dist(centroid[i, 0], centroid[i, 1], centroid[j, 0], centroid[j, 1]))
            distance[j,i] = distance[i,j]


    flowtable = np.zeros((n,n))
    
    for item in data:
        oid = idtable[item[0],-1]
        iid = idtable[item[1],-1]
        flowtable[oid, iid] += item[2]

    output = []
    inoutflow = np.zeros((n,2))

    for i in range(len(flowtable)):
        for j in range(len(flowtable[:,0])):
            if (flowtable[i,j] > 0) and (i <> j):
                inoutflow[j,0] += int(flowtable[i,j])
                inoutflow[i,1] += int(flowtable[i,j])
                output.append([int(distance[i,j]), i, j, int(flowtable[i,j])])

    pop_CSV = filepath + 'census_county_pop.csv'
    pop = build_data_list(pop_CSV)
    pop2000 = pop[:,-2]
    pop1995 = pop[:,1]

    aggpop = np.zeros((n,2))
    for item in zip(idtable[:,-1], pop2000):
        #print item[0], item[1]
        aggpop[int(item[0]), 0] += item[1]
        aggpop[int(item[0]), 1] += item[1]

    #fileLoc = filepath + 'REDCAPLUS_100Region/census_migration_100regs_pop1995.csv'
    fileLoc = filepath + 'STATE/census_migration_state_pop2000.csv'
    headerstr = 'pop2000,pop2000'
    np.savetxt(fileLoc, aggpop, delimiter=',', header = headerstr, fmt = '%i')
    
    #fileLoc = filepath + 'REDCAPLUS_100Region/census_migration_100regs_inoutflow.csv'
    fileLoc = filepath + 'STATE/census_migration_state_inoutflow.csv'
    headerstr = 'inflow,outflow'
    np.savetxt(fileLoc, inoutflow, delimiter=',', header = headerstr, fmt = '%i')

    headerstr = 'dist,OrigId,destId,flowVol'
    #fileLoc = filepath + 'REDCAPLUS_100Region/census_migration_100regs_dist.csv'
    fileLoc = filepath + 'STATE/census_migration_state_dist.csv'
    np.savetxt(fileLoc, output, delimiter=',', header = headerstr, fmt = '%s')